﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DienLib.PopUp
{
    /// <summary>
    /// Interaction logic for ContextMenu.xaml
    /// </summary>
    public partial class ContextMenuFormTest : Window
    {
        List<MenuItem> mnList;
        List<TextBlock> tbList;
        List<object> icList;
        List<Border> bdList;
        public ContextMenuFormTest()
        {
            InitializeComponent();
            var icon = System.Drawing.Icon.ExtractAssociatedIcon(@"D:\HalReportConfig.xlsx");
            //Image img = new Image();
            //img.Source =
            this.Icon = new DienLib.Converter.WPFBitmapConverter().ToImageSource(icon);
            //System.Windows.Controls.ContextMenu menu = new System.Windows.Controls.ContextMenu();
            //menu.HasDropShadow = true; menu.Padding = new Thickness(0); menu.Margin = new Thickness(-5,0,0,0);
            //MenuItem item1 = new MenuItem();
            
            //but1.ContextMenu.ItemContainerStyle = this.FindResource("DienStyle") as Style;

            //System.Windows.Controls.ContextMenu menu = new System.Windows.Controls.ContextMenu();
            //menu.

            //mnList.Add(mnItem1); mnList.Add(mnItem11); mnList.Add(mnItem12); mnList.Add(mnItem2);
            //tbList.Add(tbItem1); tbList.Add(tbItem11); tbList.Add(tbItem12); tbList.Add(tbItem2);
            //icList.Add(mnItem1.Icon); icList.Add(mnItem11.Icon); icList.Add(mnItem12.Icon); icList.Add(mnItem2.Icon);
            //bdList.Add(bdItem1); bdList.Add(bdItem11); bdList.Add(bdItem12); bdList.Add(bdItem2);
            }

        /// <summary>
        /// Additional Method from User to Get Value back from PopUp
        /// </summary>
        /// <returns>String contain PopUp Result</returns>
        public static void ShowDialogs()
        {
            ContextMenuFormTest popup = new ContextMenuFormTest();
            //Window window = new Window();
            //window.Content = popup;
            //window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            //window.WindowStyle = System.Windows.WindowStyle.None;
            //window.AllowsTransparency = true;
            //window.Background = System.Windows.Media.Brushes.Transparent;
            //window.Topmost = true;
            //window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //window.MouseLeftButtonDown += Window_MouseLeftButtonDown;
            //System.Media.SystemSounds.Exclamation.Play();
            popup.ShowDialog(); // ShowDialog can be used Internal ? HA HA HA ! Any Side Effect ?
            //window.MouseLeftButtonDown -= Window_MouseLeftButtonDown;
            //return popup._result;
        }

        private void but1_Click(object sender, RoutedEventArgs e)
        {
            foreach (MenuItem item in mnList)
            {
                item.BorderThickness = new Thickness(3); item.BorderBrush = Brushes.Crimson;
            }
            foreach (TextBlock text in tbList)
            {

            }
            foreach (object icon in icList)
            {
                
            }
            foreach (Border border in bdList)
            {

            }
        }

        private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            //((sender as System.Windows.Controls.ContextMenu).Parent as StackPanel).Background = Brushes.Black;

        }

        private void mnItem11_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            ContextMenuFormTest ct = UIHelper.FindVisualParent<ContextMenuFormTest>(sender as DependencyObject);
            ct.Padding = new Thickness(0);
        }

        public static class UIHelper
        {
            /// <summary>
            /// Finds a parent of a given item on the visual tree.
            /// </summary>
            /// <typeparam name="T">The type of the queried item.</typeparam>
            /// <param name="child">A direct or indirect child of the queried item.</param>
            /// <returns>The first parent item that matches the submitted type parameter. 
            /// If not matching item can be found, a null reference is being returned.</returns>
            public static T FindVisualParent<T>(DependencyObject child)
              where T : DependencyObject
            {
                // get parent item
                DependencyObject parentObject = VisualTreeHelper.GetParent(child);

                // we’ve reached the end of the tree
                if (parentObject == null) return null;

                // check if the parent matches the type we’re looking for
                T parent = parentObject as T;
                if (parent != null)
                {
                    return parent;
                }
                else
                {
                    // use recursion to proceed with next level
                    return FindVisualParent<T>(parentObject);
                }
            }
        }

        private void mnItem1_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            MessageBox.Show("haha");
        }

        private void mnItem1_ContextMenuClosing(object sender, ContextMenuEventArgs e)
        {
            MessageBox.Show("haha");
        }

        private void ContextMenu_ContextMenuOpening_1(object sender, ContextMenuEventArgs e)
        {
            MessageBox.Show("haha");
        }

        private void mnItem2_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("haha");
        }

        private void mnItem2_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            MessageBox.Show("haha");
        }

        private void mnItem1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("haha");
        }
    }
}
